package com.example.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dao.Permission;
import com.example.dao.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 功能描述
 * 权限处理mapper
 * @author: jimmy
 * @date: 2023年12月22日 12:03
 */
@Repository
public interface PermissionMapper extends BaseMapper<Permission> {
    /**
     * 根据角色查询权限，此方法在用户登录后获取左侧目录
     * @param role
     * @return
     */
    @Select("<script>"+
            "select " +
            "    permission.id,permission.pid,permission.permission_name permissionName," +
            "    permission.display_name displayName,permission.orderNumber," +
            "    rpermission.operation " +
            "from  " +
            "    sys_roles_permissions rpermission,sys_permissions permission " +
            "where " +
            "    rpermission.role_id=#{id} and rpermission.permission_id=permission.id " +
            " and permission.menuFlag=1 order by permission.orderNumber" +
            "</script>")
    List<Permission> findPermissionsByRole(Role role);

    /**
     * 查询所有权限
     * @return
     */
    @Select("<script>"+
            "select " +
            "   id ,pid ,permission_name as permissionName," +
            "    display_name as displayName,description ," +
            "    patchString , menuFlag ,    orderNumber " +
            "from  " +
            "sys_permissions " +
            "order by orderNumber " +
            "</script>")
    List<Permission> findAllPermissions();

    /**
     * 根据权限名称查询是否有相应的权限，用于校验是否有相同权限名称
     * @param param
     * @return
     */
    @Select("<script>"+
            "select count(1) total" +
            " from " +
            "  sys_permissions " +
            "<trim prefix=\"where\" prefixOverrides=\"and\">" +
            " permission_name=#{permissionname}" +
            " <if test=\"id!=0\">" +
            "   and id!=#{id}" +
            "</if>" +
            "</trim>" +
            "</script>")
    int findCountByPermissionname(Map<String, Object> param);

    /**
     * 根据父目录id删除权限
     * @param parentid
     * @return
     */
    @Delete("<script>"+
            "delete " +
            " from " +
            "sys_permissions " +
            "where pid=#{parentid}" +
            "</script>")
    int deleteByParentId(Long parentid);

    /**
     * 根据角色id查询所有权限，及分配给角色的该权限的operation
     * @param roleId
     * @return
     */
    @Select("<script>"+
            "select " +
            "    permission.id,permission.pid,permission.permission_name permissionName," +
            "    permission.display_name displayName,permission.orderNumber,rpermission.operation " +
            "from  " +
            "  sys_permissions permission   left join sys_roles_permissions rpermission " +
            "    on   permission.id = rpermission.permission_id " +
            "and " +
            "    rpermission.role_id=#{roleId}  " +
            "  order by permission.orderNumber" +
            "</script>")
    List<Permission> findAllPermissionsByRoleId(Long roleId);

    /**
     * 根据角色id查询分配给该角色的权限id
     * @param roleId
     * @return
     */
    @Select("<script>"+
            "select " +
            "    rpermission.permission_id " +
            "from  " +
            "  sys_roles_permissions rpermission " +
            "where " +
            "    rpermission.role_id=#{roleId}  " +
            "</script>")
    List<Long> findPermissionIdsByRoleId(Long roleId);

//    @Select("<script>"+
//            "select " +
//            "    permission.patchString " +
//            "from  " +
//            "    sys_roles_permissions rpermission,sys_permissions permission " +
//            "where " +
//            "    rpermission.role_id=#{id} and rpermission.permission_id=permission.id " +
//            "    and permission.pid>0 " +
//            "</script>")
//    List<String> findPermissionPatchStringByRole(Role role);
}
